﻿using System;
using System.Collections.Generic;

namespace GameLogic.Poker
{
    public class CardDeck:ICardDeck
    {
        private readonly List<Card> cards = new List<Card>();
        private int currentCard;
        private const int DeckSize = 52;

        public CardDeck()
        {
            InitializeDeck();
            Shuffle();
        }

        public Card GetNextCard()
        {
            if (currentCard >= DeckSize)
                throw new Exception("Empty deck!");
            return cards[currentCard++];
        }

        public void Shuffle()
        {
            var rand=new Random();
            for (var i = 0; i < DeckSize; i++)
                SwapCards(rand.Next(DeckSize - 1), rand.Next(DeckSize - 1));
            currentCard = 0;
        }

        private void SwapCards(int from, int to)
        {
            var tmp = cards[from];
            cards[from] = cards[to];
            cards[to] = tmp;
        }

        private void InitializeDeck()
        {

            for (int i = 0; i < 13; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    cards.Add(new Card((Rang)i,(Suit)j));
                }
            }
        }
    }
}